#! /usr/bin/env python

#install the software and set the system when the system is installed
#system:ubuntu 10.04
#author Seapeak.Xu
#mail:xvhfeng@gmail.com

import os
import string
import sys_path

#install the soft by apt-get
# install the software by apt-get {{{
def install_by_apt_get(softs_path="/opt/sys/src/softs"):
	if not os.path.exists(softs_path):
		print "File:%s is not exist." % softs_path 
		print "Skip the install soft by apt-get."
		return
	if not os.path.isfile(softs_path):
		print "arg must be file."
		print "Skip the install soft by apt-get."
		return

	f = open(softs_path,"r")
	softs = []
	try:
		softs = f.readlines()
	finally:
		f.close()
	if 0 == len(softs):
		print "There is no soft to install.skip."
	else:
		for s in softs:
			soft = s.strip()
			print "installing software:%s" % soft
			result = os.system("apt-get -yqq install " + soft)
			if 0 != result:
				print 'install software %s is error.' % soft
			else:
				print "install software %s is success." % soft
#	}}}

#install the editor vim and emacs
def install_vim(pkg_path=e"/opt/sys/pkg/"):
	vim_biz2 ="vim-7.3.tar.bz2" 
	vim_tar = "vim-7.3.tar"
	vim_folder = "vim73"
	vim_config = '''./configure --with-features=huge --enable-pythoninterp=yes --enable-gui=gnome2 --enable-cscope --enable-fontset --enable-perlinterp --enable-rubyinterp --with-python-config-dir=/usr/lib/python2.6/config'''

	if not os.path.exists(pkg_path):
		print "Path %s is not exist." % pkg_path
		return

	os.chdir(pkg_path)
	if(os.path.exists(vim_folder)):
		print "delete the old vim folder %s" % vim_folder
		os.system("rm -rf " + vim_folder)
	if os.path.exists(vim_biz2):
		print "unzip the %s pkg first" % vim_biz2
		if(0 != os.system("bzip2 -d " + vim_biz2)):
			print "bzip2 the %s is fail" % vim_biz2
	if not os.path.exists(vim_tar):
		print "the pkg %s is not exist" % vim_tar
		return

	print "untar the %s" % vim_tar
	if 0 != os.system("tar xvf " + vim_tar):
		print "untar %s is fail" % vim_tar
		return

	if not os.path.exists(vim_folder):
		print "There is no folder:%s" % vim_folder
		return

	login_name = os.getlogin()
	os.chdir(vim_folder)
	print "configing"
	os.system(vim_config)
	os.system("make")
	print "then installing the vim"
	os.system("make install")
	print "install vim is success."	
	print "begin set the vim now......"
	os.chdir("../../bin")
	you_path = "/" + login_name + "/"	
	if os.path.lexists(you_path + ".vimrc") or os.path.exists(you_path + ".vimrc"):
		os.system("rm -rf " + you_path + ".vimrc")
	if os.path.lexists(you_path + ".vim") or os.path.exists(you_path + ".vim"):
		os.system("rm -rf " + you_path + ".vim")
	os.system("ln -s /opt/sys/bin/vim/vimrc ~/.vimrc")
	os.system("ln -s /opt/sys/bin/vim ~/.vim")

def install_emacs(pkg_path=e"/opt/sys/pkg/"):
	emacs_tar ="emacs-23.3.tar.gz" 
	emacs_folder = "emacs-23.3"
	emacs_config = '''./configure -enable-font-backend --with-xft --with-freetype --with-x-toolkit'''
	if not os.path.exists(pkg_path):
		print "Path %s is not exist." % pkg_path
		return	
	
	if(os.path.exists(emacs_folder)):
		print "delete the old emacs folder %s" % emacs_folder
		os.system("rm -rf " + emacs_folder)
	
	if os.path.exists(emacs_tar):
		print "unzip the %s pkg first" % emacs_tar
		if(0 != os.system("tar zxvf " + emacs_tar)):
			print "unzip the %s is fail" % emacs_tar 
			return
	if not os.path.exists(emacs_folder):
		print "There is no folder:%s" % emacs_folder
		return

	os.chdir(emacs_folder)
	print "configing"
	os.system(emacs_config)
	os.system("make bootstrap")
	print "then installing the emacs"
	os.system("make install")

def set_key_mapping(mapping_file="/opt/sys/bin/Xmodmap"):
	if not os.path.exists(mapping_file):
		print "the key-mapping file is not exist"
		return

	if not os.path.isfile(mapping_file):
		print "the key-mapping file is not file"
		return

	login_name = os.getlogin()
	you_path = "/" + login_name +"/"
	if os.path.exists(you_path + ".Xmodmap") or os.path.lexists(you_path + ".Xmodmap":
		os.system("rm -rf " + you_path + ".Xmodmap")
	os.system("ln -s "+mapping_file+" ~/.Xmodmap")

def set_awesome(script_path="/opt/sys/bin/"):
	awesome_path = "/etc/xdg/awesome/"
	if os.path.exists(awesome + "rc.lua") and not os.path.islink("rc.lua"):
		shutil.move(awesome_path + "rc.lua" , awesome_path + "rc.lua.copy")
	if os.path.lexists(awesome_path + "rc.lua") and os.path.islink("rc.lua"):
		os.system("rm -rf " + awesome_path + "rc.lua")
		os.system("ls -s " + script_path + "rc.lua " + awesome_path + "rc.lua")

	awesome_theme = "/usr/share/awesome/themes/default"
	if os.path.exists(awesome_theme + "theme.lua") and not os.path.islink(awesome_theme + "theme.lua"):
		shutil.move(awesome_theme + "theme.lua" , awesome_theme + "theme.lua.copy")
	if os.path.lexists(awesome_theme + "theme.lua") and os.path.islink(awesome_theme + "theme.lua"):
		os.system("rm -rf " + awesome_theme + "theme.lua")
		os.system("ls -s " +script_path + "theme.lua " + awesome_theme + "theme.lua")

def install_deb(pkg_path="/opt/sys/pkg/"):
	pkgs = os.listdir(pkg_path)
	for x in pkgs:
		if x.endswith(".deb"):
			print "install the %s" + %x
			os.system("chmod 777 " + x)
			os.system("dpkg -i " + x)
			print "install the %s is over." % x

def install_fcixt(script_file):
	if not os.path.exists(script_file):
		print "the install fcixt script file is not exist"
		return

	if not os.path.isfile((script_file):
		print "the install fcixt script file is not exist"
		return
	os.system("sh " + script_file)

def set_syspath(syspath=""):
	if "" == syspath or syspath.isspace():
		print "the syspath is null or empty."
		return
	sys_path.modify(path=syspath)
